{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hzy/anaconda3/anaconda/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sklearn.preprocessing\n",
    "import os\n",
    "import xgboost\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import r2_score\n",
    "from sklearn.linear_model import Lasso, LinearRegression, Ridge\n",
    "from sklearn.svm import SVR\n",
    "from dataset import CaliforniaHousingDataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = CaliforniaHousingDataset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.84000492085\n"
     ]
    }
   ],
   "source": [
    "regressor1 = xgboost.XGBRegressor(max_depth=3, n_estimators=1000)\n",
    "regressor1.fit(dataset.train_X, dataset.train_y)\n",
    "pred_y1 = regressor1.predict(dataset.test_X)\n",
    "\n",
    "print(r2_score(dataset.test_y, pred_y1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.848644923766\n"
     ]
    }
   ],
   "source": [
    "regressor2 = xgboost.XGBRegressor(max_depth=4, n_estimators=1000)\n",
    "regressor2.fit(dataset.train_X, dataset.train_y)\n",
    "pred_y2 = regressor2.predict(dataset.test_X)\n",
    "print(r2_score(dataset.test_y, pred_y2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.854361576369\n"
     ]
    }
   ],
   "source": [
    "regressor3 = xgboost.XGBRegressor(max_depth=5, n_estimators=500)\n",
    "regressor3.fit(dataset.train_X, dataset.train_y)\n",
    "pred_y3 = regressor3.predict(dataset.test_X)\n",
    "print(r2_score(dataset.test_y, pred_y3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.854999306884\n"
     ]
    }
   ],
   "source": [
    "w = [0, 1, 2]\n",
    "print(r2_score(dataset.test_y, \n",
    "               (pred_y1 * w[0] + pred_y2 * w[1] + pred_y3 * w[2]) / sum(w) ))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "regressor = xgboost.XGBRegressor(max_depth=3, n_estimators=1000)\n",
    "regressor.fit(dataset.train_X, dataset.train_y)\n",
    "pred_y = regressor.predict(dataset.test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_loss = np.abs(regressor.predict(dataset.train_X) - dataset.train_y)\n",
    "test_loss = np.abs(regressor.predict(dataset.test_X) - dataset.test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_loss = (train_loss - train_loss.mean())/(train_loss.std())\n",
    "test_loss = (test_loss - train_loss.mean())/(train_loss.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_loss_X = np.concatenate([\n",
    "        dataset.train_X, \n",
    "        np.expand_dims(regressor.predict(dataset.train_X), axis=1)\n",
    "    ], axis=1)\n",
    "\n",
    "test_loss_X = np.concatenate([\n",
    "        dataset.test_X, \n",
    "        np.expand_dims(regressor.predict(dataset.test_X), axis=1)\n",
    "    ], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.083070823888047762"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss_regressor = xgboost.XGBRegressor(n_estimators=10)\n",
    "loss_regressor.fit(train_loss_X, train_loss)\n",
    "loss_regressor.score(test_loss_X, test_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.04567686, -0.230685  , -0.13312101, ..., -0.08875597,\n",
       "       -0.1128636 , -0.12273759], dtype=float32)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss_regressor.predict(dataset.test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
